package subject

import (
	"context"
	"encoding/json"

	"github.com/blog-zero/api/admin/internal/common/errorx"
	"github.com/blog-zero/api/admin/internal/svc"
	"github.com/blog-zero/api/admin/internal/types"
	"github.com/blog-zero/rpc/cms/cmsclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type SubjectAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSubjectAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SubjectAddLogic {
	return &SubjectAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SubjectAddLogic) SubjectAdd(req *types.AddSubjectReq) (*types.AddSubjectResp, error) {
	_, err := l.svcCtx.SubjectService.SubjectAdd(l.ctx, &cmsclient.SubjectAddReq{
		CategoryId:      req.CategoryId,
		Title:           req.Title,
		Pic:             req.Pic,
		ProductCount:    req.ProductCount,
		RecommendStatus: req.RecommendStatus,
		CollectCount:    req.CollectCount,
		ReadCount:       req.ReadCount,
		CommentCount:    req.CommentCount,
		AlbumPics:       req.AlbumPics,
		Description:     req.Description,
		ShowStatus:      req.ShowStatus,
		Content:         req.Content,
		ForwardCount:    req.ForwardCount,
		CategoryName:    req.CategoryName,
	})

	if err != nil {
		reqStr, _ := json.Marshal(req)
		logx.WithContext(l.ctx).Errorf("添加专题失败,参数:%s,异常:%s", reqStr, err.Error())
		return nil, errorx.NewDefaultError("添加专题失败")
	}

	return &types.AddSubjectResp{
		Code:    "000000",
		Message: "添加专题成功",
	}, nil
}
